package Demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Text {

    public static void reflectNewInsetance() {
        Class classStudent = null;
        try {
            classStudent = Class.forName("Demo1.Student");
            Constructor constructor = classStudent.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("张三",28);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    // 反射私有属性
    public static void reflectPrivateField() {
        Class c1 = null;
        try {
            c1 = Class.forName("Demo1.Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student) c1.newInstance();
            field.set(student,"王五");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    // 反射私有方法
    public static void reflectPrivateMethod() {
        Class c1 = null;
        try {
            c1 = Class.forName("Demo1.Student");
            Method method = c1.getDeclaredMethod("function",String.class);
            Student student = (Student) c1.newInstance();
            method.setAccessible(true);
            method.invoke(student,"我是私有的方法");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        // 创建对象 调用私有的构造方法
        //reflectNewInsetance();
        // 反射私有属性
//        reflectPrivateField();
        // 反射私有方法
        reflectPrivateMethod();
    }

    public static void main1(String[] args) {
        Class c1 = null;
        try {
            c1 = Class.forName("Demo1.Student");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Class c2 = Student.class;

        Student student = new Student();
        Class c3 = student.getClass();
    }
}
